package com.lwj.liwanjia.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.content.FileProvider;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.lwj.liwanjia.R;

import java.io.File;

import constants.Constants;

/**
 * Created by Administrator on 2017/10/13.
 */

public class UploadImageDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private Activity activity;
    public String cameraPath;
    public static int CAMERA_REQUEST_CODE = 1;
    public static int ALBUM_REQUEST_CODE = 2;

    public UploadImageDialog(@NonNull Context context) {
        super(context, R.style.MyDialog);
        setContentView(R.layout.update_head_layout);
        activity = (Activity) context;
        this.context = context;
        initView();
        initClick();
    }

    private void initClick() {
        TextView takePhoto = (TextView) findViewById(R.id.take_photo);
        TextView album = (TextView) findViewById(R.id.album);
        takePhoto.setOnClickListener(this);
        album.setOnClickListener(this);
    }

    private void initView() {
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.dialog_animation);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) context.getResources().getDimension(R.dimen.x300);
        window.setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.take_photo://照相
                String state = Environment.getExternalStorageState();//判断sdk的状态
                if (state.equals(Environment.MEDIA_MOUNTED)) {//正常挂载
                    cameraPath = Constants.IMAGE_PATH+ System.currentTimeMillis() + ".png";
                    Intent intent = new Intent();
                    //打开照相机的Action
                    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                    String file_path = Constants.IMAGE_PATH;
                    File file = new File(file_path);//创建目录
                    if (!file.exists()) {
                        file.mkdirs();
                    }
                    //把文件地址转化成Uri地址
                    Uri uri = FileProvider.getUriForFile(activity, activity.getApplicationContext().getPackageName() + ".provider", new File(cameraPath));
                    // 设置系统相机拍摄照片完成后图片文件的存放地址
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                    activity.startActivityForResult(intent, CAMERA_REQUEST_CODE);
                }
                dismiss();
                break;
            case R.id.album://从本地选择
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("vnd.android.cursor.dir/image");
                activity.startActivityForResult(intent, ALBUM_REQUEST_CODE);
                dismiss();
                break;
        }
    }
}
